<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    use HasFactory;

    const POSTONLY = ['name','auth','parent_id','status','company_id'];
    protected $fillable = [
        'name',
        'auth',
        'parent_id',
        'status',
        'company_id',
    ];

    protected $appends = [
        'status_text'
    ];

    protected $casts = [
        'status' => 'boolean',
    ];


    public function getStatusTextAttribute()
    {
        return $this->status ? '启用' : '禁用';
    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    // role
    public function parent()
    {
        return $this->belongsTo(Role::class,'parent_id','id')->select('id','name');
    }

    // role
    public function roles()
    {
        return $this->hasMany(Role::class,'parent_id','id')->select('id','name');
    }

    public function users()
    {
        return $this->hasMany(User::class,'role_id','id')->select('id','name','role_id');
    }
}
